package com.itic.appbase.applications.openoffice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * office2swf转换类
 * <p>Company: itic</p>
 * @author: bianyuwu
 * @date: 2015年5月8日 上午9:06:49
 * @version: V1.0
 */
public class OfficeToSwf {
    private static final Logger logger = LoggerFactory.getLogger(OfficeToSwf.class);
    private static final String SUCCESS_MESSAGES = " 成功转换为 ";
    
    private OfficeToSwf(){
    }
    
    /**
     * 将office等文件转换为swf格式.
     * <p>支持的文件类型 microsoft office、txt、jpg、png、pdf 
     * @param filePath 要转换的文件路径
     * @return result
     * @throws InterruptedException
     */
    public static boolean toSwf(String filePath) {
        String pdfPath = filePath.substring(0, filePath.lastIndexOf("."))+ ".pdf";
        String swfPath = filePath.substring(0, filePath.lastIndexOf("."))+ ".swf";
        String type = filePath.substring(filePath.lastIndexOf("."),filePath.length()).toLowerCase();
        
        if (".PDF".equalsIgnoreCase(type) || ".PNG".equalsIgnoreCase(type) || ".JPEG".equals(type)) {
            logger.info(filePath + " 即将转换为 " + pdfPath);
            if (PdfToSwf.pdfToSwf(pdfPath, swfPath)) {// pdf2swf
                logger.info(pdfPath + SUCCESS_MESSAGES + swfPath);
                return true;
            }
        } else {
            logger.info("即将office文件转换为PDF文件，filePath: " + filePath + ",pdfPath: " + pdfPath);
            int f1 = OpenOffice2PDF.office2PDF(filePath, pdfPath);// office2pdf
            if (f1 == 1) {
                logger.info("转换失败");
            } else if (f1 == -1) {
                logger.info("找不到源文件,或者配置错误");
            } else {
                logger.info(filePath + SUCCESS_MESSAGES + pdfPath);
                if (PdfToSwf.pdfToSwf(pdfPath, swfPath)) {// pdf2swf
                    logger.info(pdfPath + SUCCESS_MESSAGES + swfPath);
                    return true;
                }
            }
        }
        return false;
    }
    
}
